package com.security.it.authentication.mobile.sms;

import com.security.it.core.enums.ValidateCodeType;
import com.security.it.core.properties.SecurityProperties;
import com.security.it.authentication.dto.ValidateCode;
import com.security.it.authentication.common.ValidateCodeGenerator;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.web.context.request.ServletWebRequest;

import java.util.Objects;

public class SmsCodeGenerator implements ValidateCodeGenerator {

    private SecurityProperties securityProperties;

    @Override
    public ValidateCode generate(ServletWebRequest request) {
        int length = securityProperties.getCode().getSms().getLength();
        String code = RandomStringUtils.randomNumeric(length);
        return new ValidateCode(code, securityProperties.getCode().getSms().getExpireIn());
    }

    @Override
    public boolean accept(ValidateCodeType type) {
        return Objects.equals(ValidateCodeType.SMS, type);
    }

    public void setSecurityProperties(SecurityProperties securityProperties) {
        this.securityProperties = securityProperties;
    }
}
